package ca.jerg.wizard;

import java.util.*;
import javax.swing.*;
import com.nexes.wizard.*;
import ca.jerg.data.*;
import ca.jerg.config.*;

public class ErgWizard
{ 
    public static void main( String[] args ) 
    {            
        ErgWizard ew = new ErgWizard();
		ew.showWizard();
        System.exit(0);
    }	
    
    private ArrayList<ErgWizardSetData> setData = null;
    private double thresholdWatts = 0.0;
    private double warmUpTime = 15.0;
    private String description = "";
    
	private WizardEx wizard;
	private ErgWizard ergWizard;
	private ErgWizardSetDescriptor setPanel;
	private ErgWizardStartDescriptor startPanel;
	private ErgWizardWarmUpDescriptor warmUpPanel;
	
    public ErgWizard()
    {
    	setData = new ArrayList<ErgWizardSetData>();
		
        wizard = new WizardEx();
        //ergWizard = new ErgWizard();
        
        wizard.setTitle("jErg Wizard");
        wizard.getDialog().setResizable(false);

        setPanel = new ErgWizardSetDescriptor(this);
        wizard.registerWizardPanel(ErgWizardSetDescriptor.IDENTIFIER,setPanel);
        
        startPanel = new ErgWizardStartDescriptor(setPanel,this);
        wizard.registerWizardPanel(ErgWizardStartDescriptor.IDENTIFIER,startPanel);        

        warmUpPanel = new ErgWizardWarmUpDescriptor(this);
        wizard.registerWizardPanel(ErgWizardWarmUpDescriptor.IDENTIFIER,warmUpPanel);
        
        wizard.setCurrentPanel(ErgWizardStartDescriptor.IDENTIFIER);
    }
    
	public void showWizard()
	{	
		boolean done = false;
		
        do
        {
        	if ( wizard.showModalDialog() != 0 )
        		return;

        	this.setDescription(
        			((ErgWizardWarmUpPanel)warmUpPanel.getPanelComponent()).getDescription());

        	this.setWarmUpTime(Double.parseDouble(
        			((ErgWizardWarmUpPanel)warmUpPanel.getPanelComponent()).getWarmUpTime()));
        	
        	if ( this.generateErgFile() == false )
        		wizard.setCurrentPanel(ErgWizardWarmUpDescriptor.IDENTIFIER);
        	else
        		done = true;
        	
        } while ( !done );
	}
	
	public boolean isShowing()
	{
		return wizard.getDialog().isShowing();
	}
	
	public void toFront()
	{
		wizard.getDialog().toFront();
	}
	
	public void requestFocus()
	{
		wizard.getDialog().requestFocus();
	}
	
    public boolean generateErgFile()
    {
    	String fileName;
    	
    	/*
    	 * where to save the ERG file
    	 */
    	JFileChooser fileDialog = new JFileChooser();
    	switch ( fileDialog.showSaveDialog(null) )
    	{
    		case JFileChooser.APPROVE_OPTION:
    			break;
    		
    		case JFileChooser.CANCEL_OPTION:
    			return false;
    			
    		case JFileChooser.ERROR_OPTION:
    			System.exit(1);
    	}

    	String name = fileDialog.getSelectedFile().getPath();    	
    	if ( !name.endsWith(".erg") )
    		fileName = name + ".erg";
    	else
    		fileName = name;
    	    	
    	/*
    	 * build the ERG file
    	 */
    	ErgData ergData = new ErgData();
    	
    	double timeIndex = buildWarmUp(ergData);

    	for ( int i=0; i<setData.size(); i++ )
    	{
    		ErgWizardSetData data = (ErgWizardSetData)setData.get(i);
    		timeIndex += buildSet(timeIndex,data,ergData);
    	}
    	
    	ErgFile file = new ErgFile();
    	file.setData(ergData);
    	file.setFileName(fileName);
    	file.setDescription(description);
    	file.write(fileName);
    	
    	return true;
    }
    
    public double buildWarmUp( ErgData ergData )
    { 	
    	double load = new ErgConfigWork().getDouble(IErgConfig.KEY_REST,0) * thresholdWatts;    	
    	
    	// the start of the warm up is always at time 0
    	ergData.insert(new ErgPoint(0,(int)load));
    	
    	ergData.insert(new ErgPoint(warmUpTime,(int)load));
    	
    	return warmUpTime;
    }
    
    public double buildSet( double time, ErgWizardSetData data, ErgData ergData )
    {
    	double workLoad;
    	double workTime = 0;
    	double restLoad;
    	double restTime = 0;
    	double rampTime = 0;
    	
    	for ( int i=0; i<data.getRepeats(); i++ )
    	{
	    	workLoad = data.getWorkLoad();
	    	workTime = data.getWorkTime();
	    	restLoad = data.getRestLoad();    	
	    	restTime = data.getRestTime();
	    	rampTime = data.getRampTime();
	    	
	    	ErgDataRamp ramp = new ErgDataRamp(time,(int)restLoad,time + rampTime,(int)workLoad);
	    	ergData.insert(ramp.getData());
	    	
	    	ErgDataStep step = new ErgDataStep(time + rampTime + workTime,(int)workLoad,(int)restLoad);
	    	ergData.insert(step.getData());
	    	
	    	ergData.insert(new ErgPoint(time + rampTime + workTime + restTime,(int)restLoad));
	    	
	    	time += workTime + restTime + rampTime;
    	}
    	
    	return ((workTime + restTime) * data.getRepeats());
    }
    
    public void setThresholdWatts( double watts )
    {
    	thresholdWatts = watts;
    }
    
    public double getThresholdWatts()
    {
    	return thresholdWatts;
    }
    
    public void setWarmUpTime( double time )
    {
    	warmUpTime = time;
    }
    
    public double getWarmUpTime()
    {
    	return warmUpTime;
    }
   
    public void setDescription( String description )
    {
    	this.description = description;
    }
    
    public String getDescription()
    {
    	return description;
    }
    
    public void eraseSets( int num )
    {   		
   		setData.clear();
    }
    
    public void addSet( int index, ErgWizardSetData set )
    {
    	if ( setData.size() > index )
			setData.remove(index);
    	
    	setData.add(index,set);    	
    }
    
    public ArrayList<ErgWizardSetData> getSets()
    {
    	return setData;
    }
}
